package com.bh.extend.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bh.common.annotation.DataDictClass;
import com.bh.common.constant.Entity;
import com.bh.common.core.domain.entity.SysUser;
import com.bh.common.utils.DateUtils;
import com.bh.common.utils.SecurityUtils;
import com.bh.extend.domain.JcReceivingParty;
import com.bh.extend.mapper.JcReceivingPartyMapper;
import com.bh.extend.service.IJcReceivingPartyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 客户地址Service业务层处理
 *
 * @author slx
 * @date 2023-04-02
 */
@Service
public class JcReceivingPartyServiceImpl extends ServiceImpl<JcReceivingPartyMapper, JcReceivingParty> implements IJcReceivingPartyService {
    @Autowired
    private JcReceivingPartyMapper jcReceivingPartyMapper;

    /**
     * 查询客户地址
     *
     * @param id 客户地址主键
     * @return 客户地址
     */
    @DataDictClass
    @Override
    public JcReceivingParty selectJcReceivingpartyById(Long id) {
        return jcReceivingPartyMapper.selectById(id);
    }

    /**
     * 查询客户地址列表
     *
     * @param jcReceivingParty 客户地址
     * @return 客户地址
     */
    @DataDictClass
    @Override
    public List<JcReceivingParty> selectJcReceivingpartyList(JcReceivingParty jcReceivingParty) {
        if (jcReceivingParty.getJcOrganizationId() == null) {
            SysUser currentUser = SecurityUtils.getLoginUser().getUser();
            jcReceivingParty.setJcOrganizationId(currentUser.getDefaultOrgId());
        }
        return jcReceivingPartyMapper.selectJcReceivingpartyList(jcReceivingParty);
    }

    @Override
    public JcReceivingParty selectJcReceivingpartyListByName(String address) {
        return jcReceivingPartyMapper.selectJcReceivingpartyListByName(address);
    }

    /**
     * 新增客户地址
     *
     * @param jcReceivingParty 客户地址
     * @return 结果
     */
    @Override
    public int insertJcReceivingparty(JcReceivingParty jcReceivingParty) {
        if (jcReceivingParty.getIsDefault() != null && 1 == jcReceivingParty.getIsDefault()) {
            JcReceivingParty params = new JcReceivingParty();
            params.setCustomerId(jcReceivingParty.getCustomerId());
            params.setIsDefault(1);
            List<JcReceivingParty> olds = jcReceivingPartyMapper.selectJcReceivingpartyList(jcReceivingParty);
            if (olds != null && olds.size() > 0) {
                for (JcReceivingParty old : olds) {
                    old.setIsDefault(0);
                    jcReceivingPartyMapper.updateById(old);
                }
            }
        } else {
            jcReceivingParty.setIsDefault(0);
        }
        jcReceivingParty.setStatus(Entity.ACTIVE);
        jcReceivingParty.setDescription(jcReceivingParty.getJcZoneIdDtLabel());
        jcReceivingParty.setCreateName(SecurityUtils.getUsername());
        jcReceivingParty.setCreateTime(DateUtils.getNowDate());
        return jcReceivingPartyMapper.insert(jcReceivingParty);
    }

    /**
     * 修改客户地址
     *
     * @param jcReceivingParty 客户地址
     * @return 结果
     */
    @Override
    public int updateJcReceivingparty(JcReceivingParty jcReceivingParty) {
        if (jcReceivingParty.getIsDefault() != null && 1 == jcReceivingParty.getIsDefault()) {
            JcReceivingParty params = new JcReceivingParty();
            params.setCustomerId(jcReceivingParty.getCustomerId());
            params.setIsDefault(1);
            List<JcReceivingParty> olds = jcReceivingPartyMapper.selectJcReceivingpartyList(params);
            if (olds != null && olds.size() > 0) {
                for (JcReceivingParty old : olds) {
                    old.setIsDefault(0);
                    jcReceivingPartyMapper.updateById(old);
                }
            }
        } else {
            jcReceivingParty.setIsDefault(0);
        }
        jcReceivingParty.setModifyTime(new Date());
        jcReceivingParty.setJcOrganizationId(SecurityUtils.getOrgId());
        jcReceivingParty.setDescription(jcReceivingParty.getJcZoneIdDtLabel());
        return jcReceivingPartyMapper.updateById(jcReceivingParty);
    }

    /**
     * 批量删除客户地址
     *
     * @param ids 需要删除的客户地址主键
     * @return 结果
     */
    @Override
    public int deleteJcReceivingpartyByIds(Long[] ids) {
        return jcReceivingPartyMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除客户地址信息
     *
     * @param id 客户地址主键
     * @return 结果
     */
    @Override
    public int deleteJcReceivingpartyById(Long id) {
        return jcReceivingPartyMapper.deleteById(id);
    }
}
